package com.xz.knowledgeDuplication.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 分页工具
 *
 * @author 召
 * @date 2021-12-26 21:00:15
 */
public class PageInfoUtil {

    /**
     * 将传入的list进行分页
     *
     * @param arrayList
     * @param pageNum
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> listPageInfo(List<T> arrayList, Integer pageNum, Integer pageSize) {
        int total = arrayList.size();
        if (total > pageSize) {
            int toIndex = pageSize * pageNum;
            if (toIndex > total) {
                toIndex = total;
            }
            arrayList = arrayList.subList(pageSize * (pageNum - 1), toIndex);
        }
        Page<T> page = new Page<>(pageNum, pageSize);
        page.addAll(arrayList);
        page.setPages((total + pageSize - 1) / pageSize);
        page.setTotal(total);

        PageInfo<T> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

}
